import React from 'react';
import {
    Input,
    Switch,
    Slider,
    Select,
    Col,
    Radio,
    Checkbox,
    Button,
    DatePicker,
    InputNumber,
    Form,
    Tooltip,
    Icon,
    Row,
    Upload,
    message,
    Modal
} from 'antd';//引入组件

import {
    performanceCurrentFiledRealtionFunctionBYEvent,
    getSimpleValidationRuleForItem,
    getValue,
    setValue
} from '../common/CommonFunction'

import {
    EDITMODE_INPUT} from '../common/common'

import {Compment} from 'react';
let FormItem = Form.Item;

export class BasicFormItem extends React.Component{
    constructor(props){
        super(props);
        let self = this;
        let schema = this.props.schema;
        let finalSelectedItemList = null;
        let selectedItemList = [];

        this.state = {
            value: this.props.data, previewVisible: false, previewImage: '',
            [this.props.infosetcode + '_' + schema.code + 'SelectedItemList']: finalSelectedItemList,
        };
    }

    onBindRule(functionName, settings) {
        performanceCurrentFiledRealtionFunctionBYEvent(settings.rule,
            functionName, settings.infoSetCode, settings.itemCode,
            settings.rootComponent, settings.currentFormKey,
            getValue, setValue, settings.infoSetCreateDtos
        );
    }


    render(){
        let schema = this.props.schema;
        let self = this;
        let editing = self.props.editing === undefined ? true : self.props.editing;
        let rule = this.props.rule;
        let intiState = this.props.intistate;
        let InfoSetCode = this.props.infosetcode;
        //获取当前字段的各种简单规则
        let currentFieldValidation = getSimpleValidationRuleForItem(rule.validateDict, InfoSetCode, schema.code, schema);
       
        //收集执行事件事件相关信息
        let relationSettings = {
            rule: self.props.rule,
            infoSetCode: InfoSetCode,
            itemCode: schema.code,
            rootComponent: self.props.rootComponent,
            currentFormKey: InfoSetCode,
            infoSetCreateDtos: self.props.infoSetCreateDtos
        };
       
        let defaultValue = schema.defaultValue ? schema.defaultValue : '';
        var currentValue = self.props.isNew ? defaultValue : '';
        if (intiState[InfoSetCode + '_' + schema.code] !== undefined) {
            currentValue = intiState[InfoSetCode + '_' + schema.code]
        }

        let formItem = null;
        const {getFieldDecorator} = this.props.propform;

        switch (schema.editMode) {
            case EDITMODE_INPUT:
                //当前输入文本的正则表达式
                let patternRegex = null;
                if (currentFieldValidation.validateExpression) {
                    patternRegex = new RegExp(currentFieldValidation.validateExpression);
                }

                formItem = editing ? <FormItem>
                    {getFieldDecorator(InfoSetCode + '_' + schema.code, {
                        rules: [
                            {required: currentFieldValidation.isRequired},
                            {pattern: patternRegex},
                            {max: currentFieldValidation.maxLength},
                            {min: currentFieldValidation.minLength}],
                        initialValue: currentValue
                    })(<Input disabled={ !schema.canEdit }
                            onBlur={ self.onBindRule.bind(null, "onblur", relationSettings)}/>)}</FormItem>
                    :
                    (<div span={20}>{currentValue}</div>)
                break;
            default:
            formItem = editing ? getFieldDecorator(InfoSetCode + '_' + schema.code, {initialValue: currentValue})(
                <Input />)
                :
                <div>{currentValue}</div>
            break;
        }


        if (self.props.Rows === 2) {
            return (<Col span={12} className="item">
                <Col span={24} className="" >
                    <div className="name" title={schema.localizationName}>{ schema.localizationName }</div>
                    <Row>
                        <Col span={20} className="value">{formItem}</Col>
                    </Row>
                </Col>
            </Col>);
        } else {
            return (<Col span={8} className="item">
                <Col span={24} className="" >
                    <div className="name" title={schema.localizationName}>{ schema.localizationName }</div>
                    <Row>
                        <Col span={20} className="value">{formItem}</Col>
                    </Row>
                </Col>
            </Col>);
        }


    }

}